package com.github.edu.repair.web.controller;

import com.github.admin.edu.assembly.date.util.DateFormatUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.edu.client.common.component.BaseContextHandler;
import com.github.edu.client.common.component.SystemInformation;
import com.github.edu.mobile.driver.Device;
import com.github.edu.mobile.interfice.impl.LiteDeviceResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.util.Date;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-22
 */
@Slf4j
public abstract class BaseController extends BaseContextHandler {

    @Autowired
    private SystemInformation systemInformation;

    @ModelAttribute("webConfig")
    private SystemInformation getWebInformation(){
        return systemInformation;
    }
    /**
     * form表单提交 Date类型数据绑定
     * <功能详细描述>
     * @param binder
     * @see [类、类#方法、类#成员]
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        DateFormat dateFormat = DateFormatUtils.getDateFormat("yyyy-MM-dd HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @Autowired
    private LiteDeviceResolver resolver;

    public String index(HttpServletRequest request,String url){
        Device device=resolver.resolveDevice(request);
        if(device.isMobile()&& StringUtils.isNotBlank(url)){
            url=url.replaceFirst("index/","");
            return "mobile/"+url;
        }
        return url;
    }
}
